package cn.legend.travel.admin.authorize.service.impl;

import cn.legend.travel.admin.authorize.dao.cache.IUserCacheRepository;
import cn.legend.travel.admin.authorize.dao.persist.repository.IRoleRepository;
import cn.legend.travel.admin.authorize.dao.persist.repository.IUserRepository;
import cn.legend.travel.admin.authorize.pojo.entity.User;
import cn.legend.travel.admin.authorize.pojo.param.UserAddNewParam;
import cn.legend.travel.admin.authorize.pojo.param.UserLoginParam;
import cn.legend.travel.admin.authorize.pojo.param.UserUpdateParam;
import cn.legend.travel.admin.authorize.pojo.vo.RoleStandardVO;
import cn.legend.travel.admin.authorize.pojo.vo.UserLoginInfoVO;
import cn.legend.travel.admin.authorize.pojo.vo.UserLoginResultVO;
import cn.legend.travel.admin.authorize.pojo.vo.UserStandardVO;
import cn.legend.travel.admin.authorize.service.abs.AbstractUserService;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.authentication.CurrentPrincipal;
import cn.legend.travel.common.pojo.vo.PageData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends AbstractUserService {
    private final PasswordEncoder passwordEncoder;
    private final IUserRepository userRepository;
    private final IRoleRepository repository;
    private final IUserCacheRepository userCacheRepository;
    @Value("${travel.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Override
    public UserLoginResultVO login(UserLoginParam param) {
        UserLoginInfoVO loginInfo = userRepository.getByUsername(param.getUsername());
        checkLogin(loginInfo, param, passwordEncoder);
        int row = userRepository.updateLastLogin(loginInfo.getId(), loginInfo.getLoginCount() + 1);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "登录失败,服务器忙,请稍后再试");
        }
        UserLoginResultVO loginResult = getLoginResult(loginInfo);
        userCacheRepository.saveUserState(loginInfo.getId(), getUserState(loginInfo));
        return loginResult;
    }

    @Override
    public void logout(CurrentPrincipal principal) {
        Long id = principal.getId();
        userCacheRepository.delete(id);
    }

    @Override
    public void updatePassword(String newPwd, Long id) {
        User user = userRepository.getById(id);
        user.setPassword(passwordEncoder.encode(newPwd));
        int row = userRepository.update(user);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "服务器忙,请稍后再试");
        }
    }

    @Override
    public PageData<UserStandardVO> getUserList(Integer pageNum) {
        return userRepository.getUserList(pageNum, defaultQueryPageSize);
    }

    @Override
    public void addNew(UserAddNewParam param) {
        RoleStandardVO role = repository.getRoleById(param.getRoleId());
        if (role == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "添加失败,没有该管理员角色");
        }
        param.setPassword(passwordEncoder.encode(param.getPassword()));
        if (userRepository.addNew(param) != 1) {
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN, "服务器忙,请稍后再试");
        }
    }

    @Override
    public void update(UserUpdateParam param) {
        User user = userRepository.getById(param.getId());
        if (user == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "编辑失败,没有该管理员");
        }
        RoleStandardVO role = repository.getRoleById(param.getRoleId());
        if (role == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "编辑失败,没有该角色");
        }
        log.info(param.toString());
        if (userRepository.update(param) != 1) {
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN, "服务器忙,请稍后再试");
        }
    }

    @Override
    public void deleteById(Long id) {
        User user = userRepository.getById(id);
        if (user == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "删除失败,该用户不存在");
        }
        if (userRepository.deleteById(id) != 1) {
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN, "服务器忙,请稍后再试");
        }
    }

}
